<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>退货入库</title>
    <link rel="stylesheet" href="/sb/stock.css"/>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <script src="/js/jquery-3.3.1.min.js"></script>
    <script src="/layui/layui.js"></script>

</head>
<body>
<div class="sp-wrapper">
    <h2 class="sp-title"><span>退货合格单入库</span></h2>
    <div class="sp-tool">
        <span class="fl">
            <b>单据编号：[[${lists.orderNum}]]</b>
        </span>
        <span class="fr">
            <i id="save" class="save" th:if="${lists.status == 1}">确认入库</i>
            <i id="cancelReturn"  th:if="${lists.status == 1}" th:attr="d_id=${lists.id}">取消退货</i>
            <!--<span style="color: red" th:text="${T(com.b2c.wms.enums.EnumOrderReturnStatus).getName(lists.status)}"></span>-->
        </span>
    </div>
    <div class="sp-table">
        <form id="stockInForm" action="/order_return/upd_stock_info" method="post">
            <input type="hidden" name="returnId" th:value="${lists.id}">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>商品</th>
                <th>规格</th>
                <th>SKU</th>
                <th>数量</th>
                <th>已入库数量</th>
                <th>入库仓位</th>
            </tr>
            </thead>
            <tbody id="sp-data">
            <tr th:each="item:${lists.items}">
                <td th:text="${itemStat.index + 1}"></td>
                <td >
                    <img th:src="${item.colorImage}" width="50px" height="50px"/>
                </td>
                <td>
                    颜色：<i th:text="${item.colorValue}"></i>&nbsp;尺码：<i th:text="${item.sizeValue}"></i>
                </td>
                <td th:text="${item.skuNumber}"></td>
                <td th:text="${item.quantity}">200</td>
                <td th:text="${item.inQuantity+item.badQuantity}">0</td>
                <td >
                    <select class="locationNumber" name="locationId">
                        <option th:each="it:${item.items}" th:value="${it.locationId}" th:text="${it.locationName}"></option>
                    </select>

                    <input type="hidden" name="returnItemId"  th:value="${item.id}" />
<!--                    <input type="hidden" name="specNumber"  th:value="${item.skuNumber}" />-->
<!--                    <input type="hidden" name="specId"  th:value="${item.skuId}" />-->
<!--                    <input type="hidden" name="billNo" th:value="${lists.orderNum}">-->

<!--                    <input type="hidden" name="maxQuantity"  th:value="${item.quantity - item.inQuantity - item.badQuantity}" />-->
<!--                    <input type="number" name="quantity" class="quantity" autocomplete="off" readonly placeholder="请入库数量" th:value="${item.quantity - item.inQuantity - item.badQuantity}" />-->
                </td>
            </tr>

            </tbody>
        </table>
        </form>
    </div>
    <script>
        $(function () {
            // 保存
            $('#save').click(function () {
                var btnObj = $(this);
                var hasClick = $(this).hasClass("hasClick");
                // alert(hasClick);
                if(hasClick == true){
                    alert("保存中，请勿重复点击")
                    return ;
                }
                btnObj.addClass("hasClick");
                var stockInItems = [];//要提交的入库数据
                var returnResult = true;//是否有错误数据

                var quantity =document.getElementsByName("quantity");
                var specNumber =document.getElementsByName("specNumber");
                var maxQuantity =document.getElementsByName("maxQuantity");
                var locationId =document.getElementsByName("locationId");
                for (var i =0 ; i < specNumber.length ; i++){
                    if (quantity[i].value == ""){
                        btnObj.removeClass("hasClick");
                        alert("请输入入库数量");
                        $("input[name^='quantity']").eq(i).focus();
                        returnResult = false;
                        return;
                    } else {
                        if (quantity[i].value > maxQuantity[i].value){
                            btnObj.removeClass("hasClick");
                            alert("超出可入库数量");
                            $("input[name^='quantity']").eq(i).focus();
                            returnResult = false;
                            return;
                        }
                    }

                    // if (locationId[i].value != "" ) {
                    //     var item = {
                    //         locationName:locationNumber[i].value,
                    //         specNumber:specNumber[i].value
                    //     };
                    //     stockInItems.push(item);
                    // }else {
                    //     btnObj.removeClass("hasClick");
                    //     alert("请输入仓位");
                    //     returnResult = false;
                    //     return;
                    // }
                }

                if (returnResult == false) {
                    btnObj.removeClass("hasClick");
                    return false;
                }

                // var data = {
                //     infos:stockInItems
                // };

                btnObj.removeClass("hasClick");
                $("#stockInForm").submit();

                //检查入库仓位等校验
                // $.ajax({
                //     url: "/order_return/stock_info_check",
                //     type: "POST",
                //     dataType: "json",
                //     contentType: "application/json",
                //     data: JSON.stringify(data),
                //     success: function (res) {
                //         btnObj.removeClass("hasClick");
                //         if (res.code == 0) {
                //             $("#stockInForm").submit();
                //         } else {
                //             alert(res.msg);
                //         }
                //     }
                // });

                return false;
            });

            $("#cancelReturn").click(function () {
                let data = {
                    id: $(this).attr("d_id")
                };
                $.ajax({
                    url: "/order_return/erp_order_return_cancel",
                    type: "POST",
                    dataType: "JSON",
                    data:JSON.stringify(data),
                    contentType: 'application/json',
                    success: function (res) {
                        if (res.code == 0) {
                            alert("取消成功");
                            //location.reload(true);
                        }
                    }
                })
            });

        });
    </script>


</div>
</body>
</html>